filename = "addbook.dat"

def readBook(book):
    import os
    if os.path.exists(filename):
        store = open(filename, "r")
        for line in store:
            name = line.rstrip()
            entry = store.next().rstrip()
            book[name] = entry
        store.close()

def saveBook(book):
    store = open(filename, "w")
    for name, entry in book.items():
        store.write(name + '/n')
        store.write(entry + '/n')
    store.close()

def getChoice(menu):
    print(menu)
    choice = int(input("Select a choice (1-4): "))
    return choice

def addEntry(book):
    name = input("Enter a name: ")
    entry = input("Enter street, town, and phone number: ")
    book[name] = entry

def removeEntry(book):
    name = input("Enter a name: ")
    del(book[name])

def findEntry(book):
    name = input("Enter a name: ")
    if name in book:
        print(name, book[name])
    else: print("Sorry, no entry for: ", name)

def displayEntry(book):
    for name in book:
        print(name, book[name])

def main():
    theMenu = '''
    1) Add Entry
    2) Remove Entry
    3) Find Entry
    4) Display All
    5) Quit and Save
    '''
    theBook = {}
    readBook(theBook)
    choice = getChoice(theMenu)
    while choice != 5:
        if choice == 1:
            addEntry(theBook)
        elif choice == 2:
            removeEntry(theBook)
        elif choice == 3:
            findEntry(theBook)
        elif choice == 4:
            displayEntry(theBook)
        else: print("Invalid choice, try again")
        choice = getChoice(theMenu)
    saveBook(theBook)

if __name__ == "__main__":
    main()

